﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using System.Xml.Linq;
using ProtoType.DataStorage;
using System.IO.IsolatedStorage;
using System.IO;
using System.Collections.ObjectModel;
using ProtoType.ProtoTypeService;

namespace ProtoType
{
    public partial class FieldsList : PhoneApplicationPage
    {
        AppSettings apps = new AppSettings();
        ProtoTypeServiceClient client;
        DataStructureStore dss = new DataStructureStore();
        ObservableCollection<XElement> controls;
        string tag = "";
        string selectedfid = "";

        public FieldsList()
        {
            InitializeComponent();
            client = new ProtoTypeServiceClient();
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (NavigationContext.QueryString.TryGetValue("tag", out tag) && (NavigationContext.QueryString.TryGetValue("selectedfid", out selectedfid)))
            {
                PageTitle.Text = tag;
            }
            LoadFormControlList(selectedfid);
        }

        void LoadFormControlList(string formid)
        {
            if (apps.IsolatedStorageSavingSetting)
            {
                LoadFormcontrolList(dss.LoadformControlList(selectedfid));
            }
            if (apps.WebServiceSavingSetting)
            {
                client.LoadformControlListAsync(formid);
                client.LoadformControlListCompleted += new EventHandler<LoadformControlListCompletedEventArgs>(client_LoadformControlListCompleted);
            }
        }

        void client_LoadformControlListCompleted(object sender, LoadformControlListCompletedEventArgs e)
        {
            LoadFormcontrolList(e.Result);
        }

        void LoadFormcontrolList(ObservableCollection<XElement> controls)
        {
            List<ControlDesign> ControlList = new List<ControlDesign>();
            foreach (XElement control in controls.Elements("control"))
            {
                ControlDesign cont = new ControlDesign();
                cont.CName = control.Element("cname").Value;
                cont.Bid = "P" + control.Attribute("id").Value;
                ControlList.Add(cont);
            }
            lstfields.ItemsSource = ControlList;
        }

        private void lstfields_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lstfields.SelectedIndex != -1)
            {
                ControlDesign item = lstfields.SelectedItem as ControlDesign;
                (Application.Current as App).SelectedFieldBid = item.Bid;

                if (apps.IsolatedStorageSavingSetting)
                {
                    dss.SetBindingelements(selectedfid, tag, item.Bid, item.CName);
                    NavigationService.Navigate(new Uri("/LayOutControls.xaml?selectedfid=" + selectedfid, UriKind.Relative));
                }
                if (apps.WebServiceSavingSetting)
                {
                    client.SetBindingelementsAsync(selectedfid, tag, item.Bid, item.CName);
                    client.SetBindingelementsCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_SetBindingelementsCompleted);
                }
            }
        }

        void client_SetBindingelementsCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/LayOutControls.xaml?selectedfid=" + selectedfid, UriKind.Relative));
        }
    }
}